package com.shashi.security.login. service.impl;

import java.util.List;

import org.springframework.util.CollectionUtils;

import com.shashi.framework.base.dao.BaseDao;
import com.shashi.framework.base.service.impl.BaseServiceImpl;
import com.shashi.organization.user.User;
import com.shashi.security.login.Login;
import com.shashi.security.login.exception.LoginNotFoundException;
import com.shashi.security.login.service.LoginService;

public class LoginServiceImpl extends  BaseServiceImpl<Login> implements LoginService	{

	private BaseDao<User> userDao;
	
	public String login(String loginName, String password) throws LoginNotFoundException {
		String result = "";
		Login login = findByLoginName(loginName);
		if ( null == login )	{
			throw new LoginNotFoundException(loginName);
		}else	{
			if ( login.getPassword().equals(password) )	{
				result = "SUCCESS";
			}else	{
				result = "FAILURE";
			}
		}
		return result;
	}

	public void logout(String loginName) {
		// TODO Auto-generated method stub
		
	}

	public String register(Login login) {
		dao.add(login);
		return null;
	}

	/**
	 * @return the userDao
	 */
	public BaseDao<User> getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(BaseDao<User> userDao) {
		this.userDao = userDao;
	}

	public void deleteByLoginName(String loginName) {
		Login login = findByLoginName(loginName);
		if ( null != login )	{
			dao.delete(login.getId());
		}
	}
	
	public Login findByLoginName(String loginName)	{
		List<Login> loginList = dao.findByCriteria("loginName", loginName);
		if ( !CollectionUtils.isEmpty(loginList) )	{
			return loginList.get(0);
		}
		return null; 
	}

}
